<template>
  <div class="mainsu">
    <div>
      <div class="main_white paddingBottom20">
        <div class="mainor_form">
          <div class="mainor_te">
            <el-button @click="exportbuVisible = true"> 导出 </el-button>
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="单据类型" prop="type">
              <el-select
                v-model.trim="form.type"
                placeholder="状态"
                class="wid220"
              >
                <el-option label="所有" value="0"></el-option>
                <el-option
                  v-for="(typeList, index) in type_list"
                  :key="index"
                  :label="typeList"
                  :value="index"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="订单号" prop="order_sn">
              <el-input
                v-model.trim="form.order_sn"
                placeholder="订单号"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>

            <el-form-item label="创建时间" prop="createTime">
              <el-date-picker
                v-model="createTime"
                type="daterange"
                class="time220"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
                :picker-options="pickerOptions"
              />
            </el-form-item>
            <el-form-item label="用户" prop="uid">
              <change-user-input
                ref="changeUserInput"
                @sendValue="getUser"
                @clear="clearUid"
              ></change-user-input>
            </el-form-item>

            <div class="clearfix"></div>

            <el-form-item class="marginTop10">
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="reset"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_num boxdq">
          <div class="main_num_l box">
            <!-- <p>
              <em
                >注意</em
              >
            </p> -->
          </div>
        </div>
        <div class="main_table marginTop10" style="min-height: 550px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            :header-cell-class-name="this.$verticaline"
          >
            <el-table-column
              label="订单号"
              show-overflow-tooltip
              width="200"
              prop="order_sn"
            />
            <el-table-column
              label="用户名称"
              show-overflow-tooltip
              prop="member.nickname"
            />
            <el-table-column
              label="银联单号"
              show-overflow-tooltip
              min-width="140"
              prop="order_sn_unionpay"
            />
            <el-table-column label="单据类型" prop="status">
              <template slot-scope="scope"
                ><span :class="'type' + scope.row.type">{{
                  type_list[scope.row.type]
                }}</span></template
              >
            </el-table-column>

            <el-table-column label="订单应付" prop="order_money" />

            <el-table-column label="差额" prop="balance">
              <template slot-scope="scope">{{
                scope.row.balance ? scope.row.balance : ""
              }}</template>
            </el-table-column>
            <el-table-column label="订单实付" prop="real_money">
              <template slot-scope="scope">{{
                scope.row.real_money ? scope.row.real_money : ""
              }}</template>
            </el-table-column>
            <el-table-column label="佣金比例" prop="commission_rate">
              <template slot-scope="scope">
                {{ scope.row.commission_rate }}
              </template>
            </el-table-column>
            <el-table-column label="佣金金额" prop="commission_money" />
            <el-table-column label="创建时间">
              <template slot-scope="scope">{{
                parseTime(scope.row.created_at)
              }}</template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
    <!-- 订单导出 -->
    <el-dialog
      title="在线支付佣金账单导出"
      :visible.sync="exportbuVisible"
      class="main_dialog dialog500"
    >
      <p class="exptitle">选择导出时间</p>
      <el-date-picker
        v-model="exportTime"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        @change="dateChange"
      >
      </el-date-picker>
      <el-tooltip
        class="item"
        effect="dark"
        content="时间范围只能选择两个月之内"
        placement="top"
      >
        <i class="el-icon-question"></i>
      </el-tooltip>
      <p class="exptitle" style="padding-top: 10px">用户</p>
      <change-user-input
        ref="changeUserInput"
        @sendValue="
          (data) => {
            export_uid = data.info.user_id;
          }
        "
        @clear="export_uid = ''"
      ></change-user-input>

      <div slot="footer" class="dialog-footer">
        <el-button @click="exportbuVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleExport" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  commission_online,
  exportOnline,
} from "@/api/financial/finacialCommission";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import dateOption from "@/options/dateOption";
import { parseTime, exportMethod, getNowMonth } from "@/utils/index";

export default {
  components: {},
  data() {
    return {
      // 时间
      parseTime,
      exportMethod,
      getNowMonth,
      // 列表数据
      list: [],
      type_list: {},
      settFormVisible: false,
      listLoading: false,
      // 搜索
      form: {
        order_sn: "", //单号
        type: null, //单据状态
        start_time: "", //结算日期
        end_time: "",
        uid: "",
      },
      createTime: [],
      pickerOptions: dateOption,
      // 更多
      moredata: false,
      // 全选
      selectArr: [],
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      exportTime: [],
      export_uid: "",
      exportbuVisible: false,
    };
  },
  created() {
    this.commission_online();
  },
  methods: {
    // 订单差额 ？注释
    renderHeader_purchase(h, { column, $index }) {
      return h("div", [
        h("span", {
          domProps: {
            innerHTML: column.label,
          },
          on: {
            click: () => {
              console.log(`${column.label}   ${$index}`);
            },
          },
        }),
        h(
          "el-tooltip",
          {
            // 表示属性
            attrs: {
              effect: "dark",
              content: "注释：差异金额=下单金额-对账金额",
              placement: "top",
            },
          },
          [
            h("i", {
              style: "margin-left: 5px;margin-top:-1px;",
              class: "el-icon-question table-msg",
            }),
          ]
        ),
      ]);
    },

    //获取用户信息
    getUser(data) {
      let uid = data.info.user_id;
      this.form.uid = uid;
    },
    // 清除用户的uid
    clearUid() {
      this.form.uid = "";
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.commission_online();
    },
    // 重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.createTime = [];
      this.commission_online();
    },
    // 导出
    handleExport() {
      if (this.exportTime.length == 0) {
        this.$message.warning("请选择需要导出的时间段");
        return;
      }
      let form = {
        start_time: "",
        end_time: "",
        uid: this.export_uid,
      };

      if (this.exportTime && this.exportTime.length > 0) {
        form.start_time = this.exportTime[0];
        form.end_time = this.exportTime[1];
      }
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, form)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form
      );
      exportOnline(data).then((res) => {
        this.exportMethod(res, "在线支付佣金账单");
      });
      this.exportbuVisible = false;
      this.$nextTick(() => {
        this.exportTime = [];
        this.uid = "";
      });
    },
    // 日期选择
    dateChange(val) {
      let start = val ? new Date(val[0]).getTime() : 0;
      let end = val ? new Date(val[1]).getTime() : 0;
      let diff = start - end;
      if (Math.abs(diff) > 3600 * 1000 * 24 * 60) {
        this.$message.warning("选择时间跨度不能超过两个月");
        this.exportTime = this.getNowMonth();
      }
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.commission_online();
    },
    changePage(page) {
      this.current.page = page;
      this.commission_online();
    },

    // 列表
    commission_online() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      commission_online(data).then((res) => {
        if (res.errcode == 0) {
          this.listLoading = false;
          this.list = res.data.list.data;
          this.totalCount = res.data.list.total;
          this.type_list = res.data.type_list;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.createTime[0] ? this.createTime[0] : "";
      form.end_time = this.createTime[1] ? this.createTime[1] : "";

      return form;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 180px);
  }

  .main_dialog {
    .main_xq {
      /deep/.el-form-item--small.el-form-item {
        margin-bottom: 0;
      }
    }

    .main_table {
      /deep/.el-table__body-wrapper {
        overflow-y: auto;
        height: 400px;
      }
      /deep/.el-table--small td {
        padding-top: 0;
      }
      /deep/.el-form-item--small.el-form-item {
        margin-bottom: 16px;
        margin-top: 16px;
        margin-right: 0;
      }
    }
    .main_xq ul li {
      width: 33.33%;
      margin-top: 5px;
      margin-bottom: 5px;
      .main_xqs {
        width: 85%;
        display: block;
        text-align: left;
        /deep/.el-input--small {
          width: 100%;
        }
      }
    }
    .footer_red {
      font-size: 16px;
      color: red;
      margin-right: 10px;
    }
  }
}
// 支付状态
.type2 {
  color: $colour_red;
}
.type1 {
  color: $green;
}
// 订单状态
.order0 {
  color: $order_color4;
}
.order1 {
  color: $order_color1;
}

/deep/.main_table .el-table--small td.handle .cell {
  display: flex;
  flex-direction: row;
  align-items: center;
}
// 导出订单表头
.exptitle {
  display: block;
  font-size: 16px;
  color: #000000;
  margin: 0 0 10px;
  cursor: pointer;
}
</style>
